;___________________________________________________________________________________________________
;---------------------------------------------------------------------------------------------------
;	File: PM App Launcher Install
;---------------------------------------------------------------------------------------------------
;	Group: Overview
;		Installer & updater for PM App Launcher and all of the included
;		apps. Checks for currently installed PMAL in documnents folder
;		and compares version of each file to current release version.
;	Group: About
;		General script information
;		
;		- *Written By:*		Rameen Bakhtiary
;		- *Date Modified:*	11/30/2011
;---------------------------------------------------------------------------------------------------
 #NoEnv
 #SingleInstance, force
 SetWorkingDir, %A_ScriptDir%
 DetectHiddenWindows, On
 SetTitleMatchMode, 2
;___________________________________________________________________________________________________
;---------------------------------------------------------------------------------------------------



;*****************************************************************
;					S C R I P T    S E T T I N G S
;
;
	
	;_______________________
	; 		INSTALL LOCATION
	;
	INSTALL_DIR 		=	%A_MyDocuments%\PM App Launcher	
	
	
	;____________________________
	; 		PUBLIC FILE LOCATIONS
	;
	EPUB			=	\\corp.halliburton.com\team\WD\Business Development and Technology\General\Engineering Public
	APPDATA			=	%EPUB%\PM App Launcher\App Data\Internal
	VER_INI_NAME	=	versioncheck.ini
	
;_________________________________________________________________
;*****************************************************************







;______________________
; 		SET TRAY ICON
;
	if A_IsCompiled
		menu, tray, icon, % A_ScriptFullPath, -159





;_____________________________________________
; 		CHECK FOR FIRST TIME INSTALLATION
;
	IfNotExist, %INSTALL_DIR%
	{	firstInstall := True
		goto checkUpdates
	}




;____________________________________________
;		GET VERSION NUMBER OF ALL USER'S APPS
;
	FileGetVersion, cwiSBver, %INSTALL_DIR%\Include\CWI SB\CWI Search Bar.exe
	FileGetVersion, cwiSettingVer, %INSTALL_DIR%\Include\CWI SB\CWI Settings.exe
	FileGetVersion, specFinderVer, %INSTALL_DIR%\Include\Spec Finder\specFinder.exe
	FileGetVersion, ThreadsVer, %INSTALL_DIR%\Include\Threads\Threads.exe
	FileGetVersion, TitlerVer, %INSTALL_DIR%\Include\Titler\CWI Title Creator.exe
	FileGetVersion, PMlauncherVer, %INSTALL_DIR%\PM Application Launcher.exe
	sleep 100




;____________________________________________________
;		GET VERSION NUMBER OF ALL INSTALLER FILE APPS
;
	VERSION_INI 	= 	%APPDATA%\%VER_INI_NAME%
	
	IniRead, cwiSBInstver, %VERSION_INI%, CWI Search Bar, Version
	IniRead, cwiSettingInstver, %VERSION_INI%, CWI Settings, Version
	IniRead, specFinderInstVer, %VERSION_INI%, Spec Finder, Version
	IniRead, ThreadsInstVer, %VERSION_INI%, Threads, Version
	IniRead, TitlerInstVer, %VERSION_INI%, CWI Title Creator, Version
	IniRead, PMlauncherInstVer, %VERSION_INI%, PM App Launcher, Version




;___________________________________________________
; 		CHECK FOR UTILITY FILES - INSTALL IF MISSING
;

	;--- TASKKILL ---
	FileDelete, %INSTALL_DIR%\Resource\taskkill.exe
	Sleep, 50	
	FileInstall, Include\Util\taskkillXP.exe, %INSTALL_DIR%\Resource\taskkillXP.exe, 1





;_____________________________________________________________
; 		CHECK IF EACH (USER) APP DOESN'T EXIST  / NEEDS UPDATE
;		AND GO TO UPDATE ROUTINES ONE AT A TIME
;
	checkUpdates:
	
	  ;--- CWI SB ---
	if (!FileExist(INSTALL_DIR "\Include\CWI SB\CWI Search Bar.exe") || cwiSBInstVer>cwiSBVer || cwiSettingInstVer>cwiSettingVer || firstInstall){
		gosub, cwiSB
		sleep 200
		
	} ;--- SPEC FINDER ---
	if (!FileExist(INSTALL_DIR "\Include\Spec Finder\specFinder.exe") || specFinderInstVer>specFinderVer || firstInstall){
		gosub, specFinder
		sleep 200
		
	} ;--- THREADS ---
	if (!FileExist(INSTALL_DIR "\Include\Threads\Threads.exe") || threadsInstVer>threadsVer || firstInstall){
		gosub, Threads
		sleep 200
		
	} ;--- TITLER ---
	if (!FileExist(INSTALL_DIR "\Include\Titler\CWI Title Creator.exe") || titlerInstVer>titlerVer || firstInstall){
		gosub, titler
		sleep 200
		
	} ;--- PM APPLICATION LAUNCHER ---
	if (!FileExist(INSTALL_DIR "\PM App Launcher.exe") || PMlauncherInstVer>PMlauncherVer || firstInstall){
		gosub, PMlauncher
		sleep 200
	}
	
	
	

;___________________________________________________
; 		MAKE SURE THAT 'MOVE PDF' REG IS CORRECT
;
	gosub, pdfReg
	sleep 200




;________________________________________________________
; 		WRITE THE VERSION AND INSTALL LOCATION TO REG
;		(even tho they aren't really used anymore)
;
	RegWrite, REG_SZ, HKCU, Software\PM App Launcher, Version, %PMlauncherInstVer%
	RegWrite, REG_SZ, HKCU, Software\PM App Launcher, INSTALL_DIR, %INSTALL_DIR%




;_________________________________________________________
; 		NOTIFY OF COMPLETION; RUN PM LAUNCHER AND EXIT
;

	;~ TrayTip, PM App Launcher, PM App Launcher has been upgraded`n`nto version %pmlauncherinstver%`n
	run, %INSTALL_DIR%\PM Application Launcher.exe
	sleep 800
	ExitApp




;							--------- Script End -----------












;____________________________________ APPLICATION UPDATE ROUTINES __________________________________
;%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%






;===================================================================================================
;	
;									P M   A P P   L A U N C H E R
;
;===================================================================================================
pmLauncher:	
	;____________________________________________
	; 		CLOSE PM App Launcher IF RUNNING
	;
	progName = PM Application Launcher.exe
	SendMessage, 0x111, 65307,,, %progName%
	sleep 100
	Process, close,	%progName%
	sleep 200
	process, exist, %progName%
	If ErrorLevel
	{	;Couldnt close
		msgbox Please exit %progName% then press OK
		goto pmLauncher
	}
	
	
	;___________________
	; 		DELETE FILES
	;
	FileDelete, %INSTALL_DIR%\PM Application Launcher.exe
	sleep 100
	FileDelete, %INSTALL_DIR%\Resource\PML_*
	sleep 100
	FileDelete, %a_startup%\PM App Launcher.lnk
	sleep 100
	FileDelete, %a_startup%\PM Application Launcher.lnk
	sleep 100
	

	
	
	;__________________________________________
	; 		CREATE FOLDER INCASE NOT EXIST
	;
	FileCreateDir, %INSTALL_DIR%
	sleep 100
	FileCreateDir, %INSTALL_DIR%\Resource
	sleep 100
	
	
	;_______________________
	; 		INSTALL FILES
	;
	FileInstall, PM Application Launcher.exe, %INSTALL_DIR%\PM Application Launcher.exe, 1
	sleep 150
	IfNotExist, %INSTALL_DIR%\PM Application Launcher.exe	;Just in case it doesnt go the 1st time...
	{
		FileInstall, PM Application Launcher.exe, %INSTALL_DIR%\PM Application Launcher.exe, 1
		Sleep 100
	}
	FileInstall, Resource\PML_title.PNG, %INSTALL_DIR%\Resource\PML_title.png, 1
	sleep 100
	FileInstall, Resource\PML_sides.PNG, %INSTALL_DIR%\Resource\PML_sides.png, 1
	sleep 100
	FileInstall, Resource\PML_separate.PNG, %INSTALL_DIR%\Resource\PML_separate.png, 1
	sleep 100
	FileInstall, Resource\PML_ButtonBG.PNG, %INSTALL_DIR%\Resource\PML_ButtonBG.png, 1
	sleep 100
	FileInstall, Resource\PML_bottom.PNG, %INSTALL_DIR%\Resource\PML_bottom.png, 1
	sleep 100
	FileInstall, Resource\PML_about.txt, %INSTALL_DIR%\Resource\PML_about.txt, 1
	sleep 100
	
	
	;_______________________________________
	; 		CREATE SHORTCUTS AND AUTORUNS
	;
	FileCreateShortcut, %INSTALL_DIR%\PM Application Launcher.exe, %a_startup%\PM App Launcher.lnk
	sleep 100
	FileCreateShortcut, %INSTALL_DIR%\PM Application Launcher.exe, %A_StartMenu%\PM App Launcher.lnk
	Sleep 100 	
return






;===================================================================================================
;	
;					C W I   S E A R C H   B A R
;
;===================================================================================================
cwiSB:
	
	;____________________________________________
	; 		CLOSE CWI SEARCH BAR IF RUNNING
	;
	wasRunning := False
	progName = CWI Search Bar.exe
	Process, exist, %progname%
	If (ErrorLevel){
		wasRunning := True
		SendMessage, 0x111, 65307,,, %progName%
		sleep 100
		Process, close,	%progName%
		sleep 200
		process, exist, %progName%
		If ErrorLevel
		{	;Couldnt close
			msgbox Please exit %progName% then press OK
			goto cwiSB
		}
	}
	
	
	;___________________
	; 	DELETE FILES
	;
	sleep 150
	FileDelete, %INSTALL_DIR%\Include\CWI SB\CWI Search Bar.exe
	sleep 150
	FileDelete, %INSTALL_DIR%\Include\CWI SB\CWI Settings.exe
	sleep 100
	FileDelete, %INSTALL_DIR%\Include\CWI SB\Res\cal1.bmp
	sleep 100
	FileDelete, %INSTALL_DIR%\Include\CWI SB\Res\CWIhelp.txt
	sleep 100
	FileDelete, %INSTALL_DIR%\Resource\CSB_cal1.bmp
	sleep 100
	FileDelete, %INSTALL_DIR%\Resource\CSB_help.txt
	sleep 100
	
	
	;__________________________________________
	; 		CREATE FOLDER INCASE NOT EXIST
	;
	FileCreateDir, %INSTALL_DIR%\Include\CWI SB
	sleep 100
	FileCreateDir, %INSTALL_DIR%\Resource
	sleep 100

	
	
	;_______________________
	; 		INSTALL FILES
	;
	FileInstall, Include\CWI SB\CWI Search Bar.exe, %INSTALL_DIR%\Include\CWI SB\CWI Search Bar.exe, 1
	sleep 200
	FileInstall, Include\CWI SB\CWI Settings.exe, %INSTALL_DIR%\Include\CWI SB\CWI Settings.exe, 1
	sleep 100
	FileInstall, Resource\CSB_cal1.bmp, %INSTALL_DIR%\Resource\CSB_cal1.bmp, 1
	sleep 100
	FileInstall, Resource\CSB_help.txt, %INSTALL_DIR%\Resource\CSB_help.txt, 1
	sleep 100

	
	
	;_______________________________________
	; 		CREATE SHORTCUTS AND AUTORUNS
	;
	FileCreateShortcut, %INSTALL_DIR%\Include\CWI SB\CWI Search Bar.exe, %a_startup%\CWI Search Bar.lnk
	sleep 100
	FileCreateShortcut, %INSTALL_DIR%\Include\CWI SB\CWI Search Bar.exe, %A_StartMenu%\CWI Search Bar.lnk
	Sleep 100 	
	
		
	;______________________________________________________________________
	; 		IF FIRST TIME INSTALL -- CHECK FOR CALIBRATION FROM RTA MANAGER
	;
	IfExist, %A_MyDocuments%\Halliburton RTA Manager\Include\calibrationSettings.ini  ;--  Yes... calibration is mis-spelled in the RTA Manager file...
	{	
		FileCopy, %A_MyDocuments%\Halliburton RTA Manager\Include\calibrationSettings.ini, %INSTALL_DIR%\Include\CWI SB\callibrationSettings.ini, 1
		Sleep 100		
	}	
	sleep 100
	
	
	;________________________________________________________
	; 		RE-RUN CWI SEARCH BAR IF IT WAS RUNNING B4 UPDATE
	;
	if wasRunning
	{	run, %INSTALL_DIR%\include\cwi sb\cwi search bar.exe
		sleep 100
	}	
	
return






;===================================================================================================
;	
;					S P E C   F I N D E R
;
;===================================================================================================
specFinder:	
	
	;____________________________________________
	; 		CLOSE SPECFINDER IF RUNNING
	;
	progName = specFinder.exe
	SendMessage, 0x111, 65307,,, %progName%
	sleep 100
	Process, close,	%progName%
	sleep 200
	process, exist, %progName%
	If ErrorLevel
	{	;Couldnt close
		msgbox Please exit %progName% then press OK
		goto specFinder
	}
	
	;___________________
	; 		DELETE FILES
	;	
	FileDelete, %INSTALL_DIR%\Include\Spec Finder\specFinder.exe
	sleep 100
	
	
	;__________________________________________
	; 		CREATE FOLDER INCASE NOT EXIST
	;
	FileCreateDir, %INSTALL_DIR%\Include\Spec Finder
	sleep 100
	
	
	;_______________________
	; 		INSTALL FILES
	;
	FileInstall, Include\Spec Finder\specFinder.exe, %INSTALL_DIR%\Include\Spec Finder\specFinder.exe, 1
	sleep 100	
return







;===================================================================================================
;	
;					T H R E A D S
;
;===================================================================================================
threads:
	
	;____________________________________________
	; 		CLOSE THREADS IF RUNNING
	;
	progName = Threads.exe
	SendMessage, 0x111, 65307,,, %progName%
	sleep 100
	Process, close,	%progName%
	sleep 200
	process, exist, %progName%
	If ErrorLevel
	{	;Couldnt close
		msgbox Please exit %progName% then press OK
		goto threads
	}
	
	;___________________
	; 		DELETE FILES
	;		
	FileDelete, %INSTALL_DIR%\Include\threads\Threads.exe
	sleep 100
	FileDelete, %INSTALL_DIR%\Include\threads\Thread Help.txt
	sleep 100
	FileDelete, %INSTALL_DIR%\Resource\THD_Help.txt
	sleep 100
	

	
	;__________________________________________
	; 		CREATE FOLDER INCASE NOT EXIST
	;
	FileCreateDir, %INSTALL_DIR%\Include\Threads
	sleep 100
	FileCreateDir, %INSTALL_DIR%\Resource
	sleep 100
	
	
	;_______________________
	; 		INSTALL FILES
	;
	FileInstall, Include\Threads\Threads.exe, %INSTALL_DIR%\Include\Threads\Threads.exe, 1
	sleep 100		
	FileInstall, Resource\THD_Help.txt, %INSTALL_DIR%\Resource\THD_Help.txt, 1
	sleep 100	
return







;===================================================================================================
;	
;					C W I   T I T L E   C R E A T O R
;
;===================================================================================================
titler:
	
	;____________________________________________
	; 		CLOSE TITLER IF RUNNING
	;
	progName = CWI Title Creator.exe
	SendMessage, 0x111, 65307,,, %progName%
	sleep 100
	Process, close,	%progName%
	sleep 200
	process, exist, %progName%
	If ErrorLevel
	{	;Couldnt close
		msgbox Please exit %progName% then press OK
		goto titler
	}
	
	;___________________
	; 		DELETE FILES
	;		
	FileDelete, %INSTALL_DIR%\Include\Titler\CWI TItle Creator.exe
	sleep 100
	
	
	;__________________________________________
	; 		CREATE FOLDER INCASE NOT EXIST
	;
	FileCreateDir, %INSTALL_DIR%\Include\Titler
	sleep 100
	
	
	
	;_______________________
	; 		INSTALL FILES
	;
	FileInstall, Include\Titler\CWI Title Creator.exe, %INSTALL_DIR%\Include\Titler\CWI Title Creator.exe, 1
	sleep 100			
return






;===================================================================================================
;	
;			M O V E   P D F   T O   D R A W I N G S   R E G I S T R Y
;
;===================================================================================================
pdfReg:
	;______________________________
	; 		FIX PDF MOVE REGISTRY
	;
	RegWrite, REG_SZ, HKCR, AcroExch.Document.7\shell\PM Tools>>Copy to DWG folder, icon, \\corp.halliburton.com\team\WD\Business Development and Technology\General\Engineering Public\drawings\Resources\PM Appl Launcher.ico
	sleep 150
	RegWrite, REG_SZ, HKCR, AcroExch.Document.7\shell\PM Tools>>Copy to DWG folder\command,,`"\\corp.halliburton.com\team\WD\Business Development and Technology\General\Engineering Public\drawings\PM Tools-move pdf.exe`" `"`%1`"
	sleep 150	
return





;______________________________  End of File  __________________________________________
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


;----------
sleep 600
ExitApp
;----------










